﻿using Coldairarrow.Business.DataView;
using Coldairarrow.Entity.DataView;
using Coldairarrow.Entity.DTO;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using LinqKit;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.DataView
{
    [Route("/DataView/[controller]/[action]")]
    public class blade_visualController : BaseApiController
    {
        #region DI
        readonly IOperator _operator;
        public blade_visualController(Iblade_visualBusiness blade_visualBus, IOperator @operator)
        {
            _blade_visualBus = blade_visualBus;
        }
        Iblade_visualBusiness _blade_visualBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<blade_visualDTO>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _blade_visualBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<blade_visual> GetTheData(IdInputDTO input)
        {
            return await _blade_visualBus.GetTheDataAsync(input.id);
        }
        /// <summary>
        /// 查询所有(用于下拉显示)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<blade_visual>> GetAllData()
        {
            return await _blade_visualBus.GetDataListAsync();
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(blade_visual data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);
                //data.create_user = _operator.UserId;
                //data.create_dept = _operator?.Property.DepartmentId;
                data.create_time = DateTime.Now;
                data.is_deleted = 0;
                await _blade_visualBus.AddDataAsync(data);
            }
            else
            {
                //data.update_user = _operator.UserId;
                data.update_time = DateTime.Now;
                await _blade_visualBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        
        public async Task DeleteData(List<string> ids)
        {
            ////先删除子表数据
            //var where = LinqHelper.True<blade_visual_config>();
            //if (!ids.IsNullOrEmpty())
            //{
            //    var newWhere = DynamicExpressionParser.ParseLambda<blade_visual_config, bool>(
            //            ParsingConfig.Default, false, $@"visual_id.Equals (@0)", ids);
            //    where = where.And(newWhere);
            //}
            //await _blade_visual_configBusiness.DeleteOtherIdDataAsync(where);
            await _blade_visualBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}